/*
 * SignIn.java
 *
 * This file is part of Frontgammon.
 *
 * Frontgammon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Frontgammon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Frontgammon.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Created on September 22, 2007, 9:04 PM
 */

package edu.lamar.frontgammon.client.gui;

import edu.lamar.frontgammon.client.*;
import edu.lamar.frontgammon.protocol.*;
import java.io.IOException;

import edu.lamar.frontgammon.game.*;

/**
 *
 * @author  Nitesh  & Amatya
 */
public class SignIn extends javax.swing.JFrame {
    public Client cli;
    public MessageBox msgBox;    
    public GameGUI ggui;
    /** Creates new form SignIn */
    public SignIn() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        title = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        userText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        passText = new javax.swing.JTextField();
        siginBut = new javax.swing.JButton();
        registerBut = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sign In");
        title.setFont(new java.awt.Font("Arial", 0, 18));
        title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        title.setText("Sign In");

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setText("Please sign in below.");
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setAutoscrolls(false);
        jTextArea1.setBorder(null);
        jTextArea1.setOpaque(false);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setLabelFor(userText);
        jLabel1.setText("Username:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setLabelFor(passText);
        jLabel2.setText("Password:");

        siginBut.setText("Sign In");
        siginBut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                siginButMouseClicked(evt);
            }
        });

        registerBut.setText("Register");
        registerBut.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                registerButMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(siginBut)
                                .addGap(3, 3, 3)
                                .addComponent(registerBut))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(passText)
                                    .addComponent(userText, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(27, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                .addGap(51, 51, 51))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(passText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(siginBut)
                    .addComponent(registerBut))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void startGame() {
         if(msgBox.isVisible())        
            msgBox.setVisible(false);
        
        ggui.cli = cli;
        ggui.msgBox = msgBox;
        ggui.setGameState();        
        this.setVisible(false);
    }
    
    public void invalidLogin() {
        if(msgBox.isVisible())        
            msgBox.setVisible(false);
        msgBox.showMessage(userText.getText(),"Error: Invalid Login");
        userText.setEnabled(true);
        passText.setEnabled(true);
        registerBut.setEnabled(true);
        siginBut.setEnabled(true);
    }
    
    public void waitingOpponent() {
        if(msgBox.isVisible())        
            msgBox.setVisible(false);
        msgBox.showMessage(userText.getText(),"Welcome,Please wait for an opponent to join\nThe game starts whenever an opponent is available");
    }
    
    private void registerButMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_registerButMouseClicked

        Object[] params = { -1, -1, userText.getText(), passText.getText() };
        Message msg = new Message ( Command.Register, params);
        try {
            cli.sendToServer(msg);            
            
            userText.setEnabled(false);
            passText.setEnabled(false);
            registerBut.setEnabled(false);
            siginBut.setEnabled(false);
            msgBox.showMessage(userText.getText(),"Registration Request sent to server \n Please Wait for Response..");            
        } catch (IOException ex) {
            msgBox.showMessage("Error"," Register Request Sending Error");            
            //ex.printStackTrace();
        }
        
    }//GEN-LAST:event_registerButMouseClicked

    private void siginButMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_siginButMouseClicked
        Object[] params = { -1, -1, userText.getText(), passText.getText() };
        Message msg = new Message ( Command.Login, params);
        try {
            cli.sendToServer(msg);
            userText.setEnabled(false);
            passText.setEnabled(false);
            registerBut.setEnabled(false);
            siginBut.setEnabled(false);
            msgBox.showMessage(userText.getText(),"Your Login Request sent to server \n Please Wait for Response..");
        } catch (IOException ex) {
            msgBox.showMessage("Error","Login Request Sending Error");            
            //ex.printStackTrace();
        }
    }//GEN-LAST:event_siginButMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField passText;
    private javax.swing.JButton registerBut;
    private javax.swing.JButton siginBut;
    private javax.swing.JLabel title;
    private javax.swing.JTextField userText;
    // End of variables declaration//GEN-END:variables
    
}
